﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using Office = Microsoft.Office.Core;
using Microsoft.Office.Tools.Excel;
using System.Reflection;
using System.IO;

namespace LightingServer.TemplateGenerator
{
    class Program
    {
        protected const string FacadeXpath = "/灯具信息/图表/外观";
        protected const string SizeXpath = "/灯具信息/图表/尺寸";
        protected const string GradingXpath = "/灯具信息/图表/配光";
        protected const string LiveXpath = "/灯具信息/图表/实景";

        static void Main(string[] args)
        {
            string server = "localhost";
            string database = "zcdb";
            string uid = "root";
            string password = "";
            string connections= "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

            LightingServerContext.ConnectionStrings = connections;
            LightingServerContext.ImageRoot = @"F:\Images";
            LightingServerContext.ImageUrl = "http://127.0.0.1/pic/";
            LightingServerContext.TemplateRoot = @"F:\Template";

            QueryService service = new QueryService();
            Lightings lightings = service.Query(new LightingQuery.Builder().Build());
            //ListGenerator generator = new ListGenerator();
            //byte[] value = generator.Generate(lightings);

            SpecAllGenerator generator1 = new SpecAllGenerator();
            byte[] value = generator1.Generate(lightings.LightingList[0], true);

            //SpecGenerator generator = new SpecGenerator();
            //byte[] value = generator.Generate(lightings.LightingList[0]);

            if (value != null)
            {
                //string file = "F:/test.xlsx";
                string file = "F:/test.pdf";
                if (File.Exists(file))
                    File.Delete(file);

                using (FileStream fs = File.Create(file))
                {
                    fs.Write(value, 0, value.Length);
                    fs.Flush();
                }
            }

            Console.ReadLine();
            
        }

        private static string CopyTemplate(string sourceName)
        {
            string path = null;
            Assembly asm = Assembly.GetExecutingAssembly();
            using (Stream stream = asm.GetManifestResourceStream("LightingServer.TemplateGenerator." + sourceName))
            {
                path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".xlsx");
                using (Stream fileStream = File.Create(path))
                {
                    stream.CopyTo(fileStream, 1024);
                }
            }
            return path;
        }

        private static Excel.XmlMap SearchMap(Excel.Workbook wb, string mapName)
        {
            foreach (Excel.XmlMap xmlMap in wb.XmlMaps)
            {
                if (xmlMap.Name == mapName)
                    return xmlMap;
            }
            return null;
        }


    }

}
